//
//  KKVoiceView.m
//  DoMi
//
//  Created by 康凯 on 2016/12/9.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKVoiceView.h"

@implementation KKVoiceView

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if(self =[super initWithCoder:aDecoder]){
        
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self initView];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.frame=CGRectMake(0, screenHeight-240, screenWidth, 240);
}

-(void)initView{
    self.voiceBottom.constant=0;
    [self layoutIfNeeded];
    float btnWidth=self.voiceBtnView.frame.size.width;
    float btnHeight=self.voiceBtnView.frame.size.height;
    [self.recorderBtn addTarget:self action:@selector(recordClick:) forControlEvents:UIControlEventTouchDown];
    [self.recorderBtn addTarget:self action:@selector(recordFinish:) forControlEvents:UIControlEventTouchUpInside];
    [self.recorderBtn addTarget:self action:@selector(recordCancle:) forControlEvents:UIControlEventTouchDragExit];
    
    //第一步，通过UIBezierPath设置圆形的矢量路径
    UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0,btnWidth, btnHeight)];
    
    //第二步，用CAShapeLayer沿着第一步的路径画一个完整的环（颜色灰色，起始点0，终结点1）
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    bgLayer.frame = CGRectMake(0, 0, btnWidth, btnHeight);//设置Frame
    bgLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色=透明色
    bgLayer.lineWidth = 10.f;//线条大小
    bgLayer.strokeColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;//线条颜色
    bgLayer.strokeStart = 0.f;//路径开始位置
    bgLayer.strokeEnd = 1.f;//路径结束位置
    bgLayer.path = circle.CGPath;//设置bgLayer的绘制路径为circle的路径
    [self.voiceBtnView.layer addSublayer:bgLayer];//添加到屏幕上
    
    //第三步，用CAShapeLayer沿着第一步的路径画一个红色的环形进度条，但是起始点=终结点=0，所以开始不可见
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    self.shapeLayer=shapeLayer;
    shapeLayer.frame = CGRectMake(0, 0, btnWidth, btnHeight);
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineWidth = 10.f;
    shapeLayer.strokeColor = [UIColor colorWithHexString:@"#22C074"].CGColor;
    shapeLayer.strokeStart = 0;
    shapeLayer.strokeEnd = 0;
    shapeLayer.path = circle.CGPath;
    [self.voiceBtnView.layer addSublayer:shapeLayer];
    CGAffineTransform transform = CGAffineTransformIdentity;
    shapeLayer.affineTransform=CGAffineTransformRotate(transform, -M_PI / 2);
}

-(void)recordClick:(UIButton *)btn{
    self.recorderStatus.text=@"松开发送";
    [self.delegate recorderTouchUp];
}
-(void)recordFinish:(UIButton *)btn{
    self.recorderTime.text=@"0.0";
    self.recorderStatus.text=@"按下录音";
    [self.delegate recorderTouchInside];
}
-(void)recordCancle:(UIButton *)btn{
    self.recorderTime.text=@"0.0";
    self.recorderStatus.text=@"按下录音";
    [self.delegate recorderTouchMove];
}

-(void)setTime{
    self.recorderTime.text=[NSString stringWithFormat:@"%.2f",self.time];
}
@end
